$j=jQuery.noConflict();
$j(document).ready
(function(){
	$j("#firstsubdiv").one("click",
	function(event){
		var subdiv=$j(this).children().html();
		var subdivDivision=$j("#division").children().html();
		$j.ajax(
		{
			url:"actions/chooseSubdivForm.php",
			data: "subdivDivision="+encodeURIComponent(subdivDivision)+"&subdiv="+encodeURIComponent(subdiv),
			success: function(result)
			{
				$j("#firstsubdiv").append(result);
				$j(".subdivRes").attr("class", "firstSubdivRes");
				$j(".firstSubdivRes").css("display", "none");
				$j(".firstSubdivRes").slideDown(500);
				$j("#curSubdiv").click(function()
				{
					$j(".firstSubdivRes").slideToggle(500);
				});
				$j(".aSubdivRes").one("click", function(event)
					{
						var val=$j(this).attr("val");
						var html=$j(this).html();
						$j(".firstSubdivRes").slideUp(500, function()
						{
							$j(".curSubdiv").html(html);
							$j("#subdiv").val(val);
							$j(".subdiv").append('<input name="subdiv" id="subdiv" type="hidden" value="'+val+'">');

						});
				});

			}
		});
	});
	$j("#division").one("click",
	function(){
		var subdiv=$j(".curSubdiv").html();
		var division=$j(this).children().html();
		chooseDivision(division, subdiv);
		});
});
function chooseSubdiv(subdiv, subdivDivision)
	{
		$j.ajax(
		{
			url:"actions/chooseSubdivForm.php",
			data: "subdivDivision="+encodeURIComponent(subdivDivision)+"&subdiv="+encodeURIComponent(subdiv),
			success: function(result)
			{
				$j(".subdivRes").remove();
				$j("#subdiv").append(result);
				$j(".subdivRes").css("display", "none");
				$j(".subdivRes").slideDown(500);
				$j(".curSubdiv").click(function()
				{
					$j(".subdivRes").slideToggle(500);
				});
				$j(".aSubdivRes").one("click", function(event)
				{
					var val=$j(this).attr("val");
					var html=$j(this).html();
					$j(".subdivRes").slideUp(500, function()
					{
						$j(".curSubdiv").html(html);
						$j("input#subdiv").remove();
						$j(".subdiv").append('<input name="subdiv" id="subdiv" type="hidden" value="'+val+'">');
						$j("#subdiv").val(val);

					});

				});
			}
		});
	}
	var check2=false;
	function chooseDivision(division, subdiv)
	{
		$j("#firstsubdiv").attr("id", "subdiv");
		var check=false;
		$j.ajax
		({
			url:"actions/chooseSubdivForm.php",
			data: "division="+encodeURIComponent(division),
			success: function(result)
			{
				var check=false;
				$j("#division").append(result);
				$j(".divisionRes").css("display", "none");
				$j(".divisionRes").slideDown(500);
				$j("#subdiv").slideUp(500);
				$j("#subdiv").html("<div class=\"curSubdiv\">"+subdiv+"</div>");
				$j(".divisions").one("click", function(event)
				{
					division=$j(this).html();
					divisionIsChosen(division);
					check=true;
				});
				if(!$j(".division").html()=="") //если регион выбран
				{
					$j(".curDivision").click(function()
					{
						$j(".divisions").slideToggle(500);
						$j(".curSubdiv").slideDown(500);//css("display", "block");
						$j("#subdiv").slideToggle(500);

					});
					$j("#subdiv").one("click", function(event)
					{
						if(!check && !check2)
						chooseSubdiv(subdiv, division);
					});
				}
			}
		});

	}
	function divisionIsChosen(division)
	{
		$j(".subdivRes").remove();
		$j("#divisionInput").val(division);
		$j(".curDivision").html(division);
		$j(".divisions").slideUp(500);
		var divRes=$j(".divisionRes").html();
		$j(".divisions").html("");
		$j(".curSubdiv").html("Выберите раздел рубрики");
		$j("#subdiv").slideDown(500);
		$j(".curDivision").one("click", function(event)
		{
			var division=$j(this).html();
			$j(".divisionRes").css("display", "none");
			$j(".divisionRes").html(divRes);
			$j(".divisionRes").slideDown(500);
			var subdiv="Выберите раздел рубрики";
			$j(".divisions").one("click", function(event)
			{
				division=$j(this).html();
				divisionIsChosen(division);
				check=true;
			});
			//chooseDivision(division, subdiv);
		});
			$j("#subdiv").one("click", function(event)
			{
				//if(check2)return;
				check2=true;
				var subdiv=$j(this).children().html();
				var subdivDivision=$j("#division").children().html();
				chooseSubdiv(subdiv, subdivDivision);
			});
	}